<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\CourseSectionQuestionAnswer;
use App\Models\CourseSectionQuestionAnswerBack;
use App\SensitiveWords;
use App\Services\CourseSectionQuestionAnswerBackService;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class CourseSectionQuestionAnswerBackController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'answers_id' => 'required|integer|digits_between:1,20|min:1',
            'from_users_id' => 'required|integer|digits_between:1,20|min:1',
            'to_users_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $bad_word = SensitiveWords::getBadWord($request->body);
        if (!empty($bad_word)) {
            return $this->fail(CodeResponse::PARAM_VALUE_SENSITIVE,'包含敏感词:' . current($bad_word));
        }

        CourseSectionQuestionAnswerBack::create($request->all());

        //更新回答数量
        $courseSectionQuestionAnswer = CourseSectionQuestionAnswer::find($request->answers_id);
        $answer_num= $courseSectionQuestionAnswer->answer_num+1;
        $courseSectionQuestionAnswer->find($request->answers_id)->update(['answer_num' => $answer_num]);

        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'answers_id' => 'required|integer|digits_between:1,20|min:1',
            'from_users_id' => 'required|integer|digits_between:1,20|min:1',
            'to_users_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $bad_word = SensitiveWords::getBadWord($request->body);
        if (!empty($bad_word)) {
            return $this->fail(CodeResponse::PARAM_VALUE_SENSITIVE,'包含敏感词:' . current($bad_word));
        }
        $putData = $request->except(['_method','_token']);
        $courseSectionQuestionAnswerBack = new CourseSectionQuestionAnswerBack();
        $courseSectionQuestionAnswerBack->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [

            'sort' => 'string',
            'order' => 'string',
            'answers_id' => 'integer|digits_between:1,20|min:1',
            'from_users_id' => 'integer|digits_between:1,20|min:1',
            'to_users_id' => 'integer|digits_between:1,20|min:1',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }


        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $answers_id = $request->input('answers_id');
        $from_users_id = $request->input('from_users_id');
        $to_users_id = $request->input('to_users_id');

        $list = CourseSectionQuestionAnswerBackService::getInstance()->list($sort,$order, $answers_id,$from_users_id,$to_users_id);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $back = CourseSectionQuestionAnswerBack::find($request->id);
        $back->delete();
        return $this->success();
    }
    //采纳
    public function  adopt(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'answers_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $request->offsetSet('adopt', 1); //采纳
        $putData = $request->except(['_method','_token']);

        $courseSectionQuestionAnswerBack = new CourseSectionQuestionAnswerBack();
        $courseSectionQuestionAnswerBack->find($request->id)->update($putData);

        $courseSectionQuestionAnswer = new CourseSectionQuestionAnswer();
        $courseSectionQuestionAnswer->find($request->answers_id)->update(['adopt'=>1]);
        return $this->success();
    }
}
